package cmd

import (
	"app/cmd/gen"
	"app/config"
	"errors"
	"github.com/spf13/cobra"
)

func init() {
	rootCmd.AddCommand(genCmd) //生成代码
}

var genCmd = &cobra.Command{
	Use:   "gen",
	Short: "code generation",
	Long:  "app gen code",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		//加载配置
		return config.LoadFromFile(configYml)
	},
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		if len(args) < 2 {
			return errors.New("调用参数不足！")
		}
		var name = args[1]
		var params = args[2:]

		// TODO 根据模板生成代码
		switch args[0] {
		case "service":
			err = gen.Service(name, params...)
		case "request":
			err = gen.Request(name, params...)
		case "middleware":
			err = gen.Middleware(name, params...)
		case "models":
			err = gen.Models(name, params...)
		default:
			err = errors.New("不支持的生成命令")
		}
		return
	},
}
